package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.constant.ProduceErrorCode;
import com.ruibang.glass.produce.domain.req.CommonTrendChartReq;
import com.ruibang.glass.produce.domain.req.DefectAnalysisReq;
import com.ruibang.glass.produce.domain.resp.*;
import com.ruibang.glass.produce.entity.DefectSizeAnalysis;
import com.ruibang.glass.produce.mapper.DefectSizeAnalysisMapper;
import com.ruibang.glass.produce.service.DefectSizeAnalysisService;
import com.ruibang.glass.produce.util.DataUtil;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 缺陷大小数据分析 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-04-27
 */
@Service
public class DefectSizeAnalysisServiceImpl extends ServiceImpl<DefectSizeAnalysisMapper, DefectSizeAnalysis> implements DefectSizeAnalysisService {

    @Override
    public IPage<DefectSizeAnalysis> pageQuery(DefectAnalysisReq defectAnalysisReq) {
        QueryWrapper<DefectSizeAnalysis> qw = new QueryWrapper<>();
        if (defectAnalysisReq.getStartTime() != null || defectAnalysisReq.getEndTime() != null) {
            qw.lambda().ge(DefectSizeAnalysis::getCollectTime, defectAnalysisReq.getStartTime());
            qw.lambda().le(DefectSizeAnalysis::getCollectTime, defectAnalysisReq.getEndTime());
        }
        qw.lambda().orderByDesc(DefectSizeAnalysis::getCreateTime);
        return this.page(new Page<>(defectAnalysisReq.getPageNo(), defectAnalysisReq.getPageSize()), qw.lambda());
    }

    @Override
    public DefectSizePieRateData pieRateChart(CommonTrendChartReq commonTrendChartReq) {
        //默认查询寻7天
        if (commonTrendChartReq.getStartTime() == null || commonTrendChartReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            commonTrendChartReq.setStartTime(startTime);
            commonTrendChartReq.setEndTime(endTime);
        }
        return baseMapper.pieRateChart(commonTrendChartReq);
    }

    @Override
    public DefectSizeCurveRateData curveRateChart(CommonTrendChartReq commonTrendChartReq) {
        //默认查询寻7天
        if (commonTrendChartReq.getStartTime() == null || commonTrendChartReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            commonTrendChartReq.setStartTime(startTime);
            commonTrendChartReq.setEndTime(endTime);
        }

        //获取数据
        List<DefectSizeAnalysis> data = lambdaQuery().ge(DefectSizeAnalysis::getCollectTime, commonTrendChartReq.getStartTime()).le(DefectSizeAnalysis::getCollectTime, commonTrendChartReq.getEndTime()).list();

        DefectSizeCurveRateData defectSizeCurveRateData = new DefectSizeCurveRateData();
        if (CollectionUtils.isNotEmpty(data)) {
            //采集时间
            List<String> collectTime = data.stream().map(e -> e.getCollectTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
            defectSizeCurveRateData.setCollectTime(collectTime);

            //bubble
            List<Double> bubble1 = data.stream().map(DefectSizeAnalysis::getBubbleRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setBubbleRate1(bubble1);
            List<Double> bubble2 = data.stream().map(DefectSizeAnalysis::getBubbleRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setBubbleRate2(bubble2);
            List<Double> bubble3 = data.stream().map(DefectSizeAnalysis::getBubbleRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setBubbleRate3(bubble3);
            List<Double> bubble4 = data.stream().map(DefectSizeAnalysis::getBubbleRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setBubbleRate4(bubble4);
            List<Double> bubble5 = data.stream().map(DefectSizeAnalysis::getBubbleRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setBubbleRate5(bubble5);

            //stone
            List<Double> stone1 = data.stream().map(DefectSizeAnalysis::getStoneRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setStoneRate1(stone1);
            List<Double> stone2 = data.stream().map(DefectSizeAnalysis::getStoneRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setStoneRate2(stone2);
            List<Double> stone3 = data.stream().map(DefectSizeAnalysis::getStoneRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setStoneRate3(stone3);
            List<Double> stone4 = data.stream().map(DefectSizeAnalysis::getStoneRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setStoneRate4(stone4);
            List<Double> stone5 = data.stream().map(DefectSizeAnalysis::getStoneRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setStoneRate5(stone5);

            //pt
            List<Double> pt1 = data.stream().map(DefectSizeAnalysis::getPtRate1).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setPtRate1(pt1);
            List<Double> pt2 = data.stream().map(DefectSizeAnalysis::getPtRate2).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setPtRate2(pt2);
            List<Double> pt3 = data.stream().map(DefectSizeAnalysis::getPtRate3).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setPtRate3(pt3);
            List<Double> pt4 = data.stream().map(DefectSizeAnalysis::getPtRate4).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setPtRate4(pt4);
            List<Double> pt5 = data.stream().map(DefectSizeAnalysis::getPtRate5).filter(Objects::nonNull).collect(Collectors.toList());
            defectSizeCurveRateData.setPtRate5(pt5);
        }
        return defectSizeCurveRateData;
    }

    @Override
    public List<DissolutionDefectData> defectChart(DefectAnalysisReq defectAnalysisReq) {
        if (defectAnalysisReq == null || StringUtils.isBlank(defectAnalysisReq.getType())) {
            throw new ServiceException(ProduceErrorCode.DEFECT_TYPE_IS_NULL.getCode(), ProduceErrorCode.DEFECT_TYPE_IS_NULL.getMsg());
        }
        //默认查询寻7天
        if (defectAnalysisReq.getStartTime() == null || defectAnalysisReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            defectAnalysisReq.setStartTime(startTime);
            defectAnalysisReq.setEndTime(endTime);
        }
        return baseMapper.defectChart(defectAnalysisReq);
    }

    @Override
    public DefectSizeBarChartResp barChart(DefectAnalysisReq defectAnalysisReq) {
        if (defectAnalysisReq == null || StringUtils.isBlank(defectAnalysisReq.getType())) {
            throw new ServiceException(ProduceErrorCode.DEFECT_TYPE_IS_NULL.getCode(), ProduceErrorCode.DEFECT_TYPE_IS_NULL.getMsg());
        }
        //默认查询寻7天
        if (defectAnalysisReq.getStartTime() == null || defectAnalysisReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(7);
            defectAnalysisReq.setStartTime(startTime);
            defectAnalysisReq.setEndTime(endTime);
        }
        List<DefectSizeAnalysis> defectSizeAnalysisList = baseMapper.barChart(defectAnalysisReq);
        DefectSizeBarChartResp defectSizeBarChartResp = new DefectSizeBarChartResp();
        //统计数据
        if (CollectionUtils.isNotEmpty(defectSizeAnalysisList)) {
            //采集时间
            List<String> collectTime = defectSizeAnalysisList.stream().map(e -> e.getCollectTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
            defectSizeBarChartResp.setCollectTime(collectTime);
            //缺陷数据统计
            switch (defectAnalysisReq.getType()) {
                case Constants.ONE_STR: {
                    //气泡
                    List<DefectSizeBarChartData> bubbleData = processBubbleBarChart(defectSizeAnalysisList);
                    defectSizeBarChartResp.setDefectDepthBarChartData(bubbleData);
                    break;
                }
                case Constants.TWO_STR: {
                    //铂金
                    List<DefectSizeBarChartData> ptData = processPtBarChart(defectSizeAnalysisList);
                    defectSizeBarChartResp.setDefectDepthBarChartData(ptData);
                    break;
                }
                case Constants.THREE_STR: {
                    //结石
                    List<DefectSizeBarChartData> stoneData = processStoneBarChart(defectSizeAnalysisList);
                    defectSizeBarChartResp.setDefectDepthBarChartData(stoneData);
                    break;
                }
                default: {
                    throw new ServiceException();
                }
            }
        }
        return defectSizeBarChartResp;
    }

    private List<DefectSizeBarChartData> processStoneBarChart(List<DefectSizeAnalysis> defectSizeAnalysisList) {
        List<DefectSizeBarChartData> data = new ArrayList<>();
        for (DefectSizeAnalysis defectSizeAnalysis : defectSizeAnalysisList) {
            DefectSizeBarChartData defectSizeBarChartData = new DefectSizeBarChartData();
            double checkTotalNum = Double.parseDouble(defectSizeAnalysis.getCheckTotalNum().toString());
            //0-150
            if (defectSizeAnalysis.getStoneNum1() != null && defectSizeAnalysis.getStoneNum1() != 0) {
                defectSizeBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getStoneNum1()));
            }
            //150-300
            if (defectSizeAnalysis.getStoneNum2() != null && defectSizeAnalysis.getStoneNum2() != 0) {
                defectSizeBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getStoneNum2()));
            }
            //300-500
            if (defectSizeAnalysis.getStoneNum3() != null && defectSizeAnalysis.getStoneNum3() != 0) {
                defectSizeBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getStoneNum3()));
            }
            //500-100
            if (defectSizeAnalysis.getStoneNum4() != null && defectSizeAnalysis.getStoneNum4() != 0) {
                defectSizeBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getStoneNum4()));
            }
            //1000
            if (defectSizeAnalysis.getStoneNum5() != null && defectSizeAnalysis.getStoneNum5() != 0) {
                defectSizeBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getStoneNum5()));
            }
            data.add(defectSizeBarChartData);
        }
        return data;
    }

    private List<DefectSizeBarChartData> processPtBarChart(List<DefectSizeAnalysis> defectSizeAnalysisList) {
        List<DefectSizeBarChartData> data = new ArrayList<>();
        for (DefectSizeAnalysis defectSizehAnalysis : defectSizeAnalysisList) {
            DefectSizeBarChartData defectSizeBarChartData = new DefectSizeBarChartData();
            double checkTotalNum = Double.parseDouble(defectSizehAnalysis.getCheckTotalNum().toString());
            //0-150
            if (defectSizehAnalysis.getPtNum1() != null && defectSizehAnalysis.getPtNum1() != 0) {
                defectSizeBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectSizehAnalysis.getPtNum1()));
            }
            //150-300
            if (defectSizehAnalysis.getPtNum2() != null && defectSizehAnalysis.getPtNum2() != 0) {
                defectSizeBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectSizehAnalysis.getPtNum2()));
            }
            //300-500
            if (defectSizehAnalysis.getPtNum3() != null && defectSizehAnalysis.getPtNum3() != 0) {
                defectSizeBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectSizehAnalysis.getPtNum3()));
            }
            //500-1000
            if (defectSizehAnalysis.getPtNum4() != null && defectSizehAnalysis.getPtNum4() != 0) {
                defectSizeBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectSizehAnalysis.getPtNum4()));
            }
            //1000
            if (defectSizehAnalysis.getPtNum5() != null && defectSizehAnalysis.getPtNum5() != 0) {
                defectSizeBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectSizehAnalysis.getPtNum5()));
            }
            data.add(defectSizeBarChartData);
        }
        return data;
    }

    private List<DefectSizeBarChartData> processBubbleBarChart(List<DefectSizeAnalysis> defectSizeAnalysisList) {
        List<DefectSizeBarChartData> data = new ArrayList<>();
        for (DefectSizeAnalysis defectSizeAnalysis : defectSizeAnalysisList) {
            DefectSizeBarChartData defectDepthBarChartData = new DefectSizeBarChartData();
            double checkTotalNum = Double.parseDouble(defectSizeAnalysis.getCheckTotalNum().toString());
            //0-150
            if (defectSizeAnalysis.getBubbleNum1() != null && defectSizeAnalysis.getBubbleNum1() != 0) {
                defectDepthBarChartData.setData1(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getBubbleNum1()));
            }
            //150-300
            if (defectSizeAnalysis.getBubbleNum2() != null && defectSizeAnalysis.getBubbleNum2() != 0) {
                defectDepthBarChartData.setData2(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getBubbleNum2()));
            }
            //300-500
            if (defectSizeAnalysis.getBubbleNum3() != null && defectSizeAnalysis.getBubbleNum3() != 0) {
                defectDepthBarChartData.setData3(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getBubbleNum3()));
            }
            //500-1000
            if (defectSizeAnalysis.getBubbleNum4() != null && defectSizeAnalysis.getBubbleNum4() != 0) {
                defectDepthBarChartData.setData4(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getBubbleNum4()));
            }
            //1000
            if (defectSizeAnalysis.getBubbleNum5() != null && defectSizeAnalysis.getBubbleNum5() != 0) {
                defectDepthBarChartData.setData5(DataUtil.formatDouble(checkTotalNum / defectSizeAnalysis.getBubbleNum5()));
            }
            data.add(defectDepthBarChartData);
        }
        return data;
    }

    public static void main(String[] args) {
        System.out.println(0 / 15);
    }
}
